﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using NHibernate;
using ParaEditor.Common;
using ParaEditor.Domain.Entities;

namespace ParaEditor.DAO.ParaDAO
{
    public class Para4004DAO:BaseDAO
    {
        private static Para4004 p4004 = new Para4004();

        public Para4004DAO()
            : base(p4004)
        {
        }

        public ObservableCollection<Para4004> GetObjectByInfoVer(int infoVer,string lineID,string OwnerStationID)
        {
            ObservableCollection<Para4004> list = new ObservableCollection<Para4004>();
            IList iList = base.GetObjectByInfoVer(infoVer);
            if (iList != null)
            {
                foreach (Para4004 item in iList)
                {
                    if (item.lineId.Trim().Equals(lineID) && item.ownerStation.Trim().Equals(OwnerStationID))
                    {
                        list.Add(item);
                    }
                }
                
            }
            return list;
        }

        public ObservableCollection<Para4004> GetObjectByInfoVer(int infoVer)
        {
            ObservableCollection<Para4004> list = new ObservableCollection<Para4004>();
            IList iList = base.GetObjectByInfoVer(infoVer);
            foreach (Para4004 item in iList)
            {
                list.Add(item);
            }
            return list;
        }

        public void Del4004ByInfoVerAndRecordID(int infover)
        {
            try
            {
                string sqlText = "delete from TBL_METRO_STATION_AREA where  info_ver = " + infover;
                ISQLQuery query = Session.CreateSQLQuery(sqlText);
                query.ExecuteUpdate();
            }
            catch (Exception e)
            {
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
        }

        public void CreateObjectNonFlushSession(Para4004 objs)
        {
            try
            {
                //Session.Merge(objs);
                Session.Save(objs);
            }
            catch (Exception e)
            {
            }
        }

        public ResultValue FlushSession()
        {
            ResultValue rValue = new ResultValue();
            try
            {
                Session.Flush();
                Session.Clear();
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            return rValue;
        }

        public ResultValue CloseSession()
        {
            ResultValue rValue = new ResultValue();
            try
            {
                Session.Close();
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            return rValue;
        }

        public ResultValue BatchCreateObject(IList<Para4004> objs)
        {
            ResultValue rValue = new ResultValue();
            //任务拆分

            try
            {
                int count = objs.Count;
                for (int i = 1; i < count + 1; i++)
                {
                    Session.Merge(objs[i - 1]);
                    if (i % 100 == 0)
                    {
                        Session.Flush();
                        Session.Clear();
                    }
                }
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
            return rValue;
        }
    }
}
